//
//  SelectJobViewController.m
//  Btpin
//
//  Created by Vic on 2019/4/21.
//  Copyright © 2019 Vic. All rights reserved.
//

#import "SelectJobViewController.h"
#import "CompanyJobModel.h"
#import "SelectJobCell.h"

@interface SelectJobViewController ()<UITableViewDelegate, UITableViewDataSource>
{
    UITableView *jobTableView;
    
    NSInteger currentPage;
    NSMutableArray *dataArray;
}
@end

@implementation SelectJobViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    dataArray = [NSMutableArray arrayWithCapacity:0];
    currentPage = 1;
    
    [self initUI];
    [self loadDataForJobList];
}
- (void)okBtnTap {
    if (dataArray.count) {
        for (CompanyJobModel *model in dataArray) {
            if (model.isSelect) {
                self.select(model.id, model.title);
                break;
            }
        }
    }
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)initUI {
    self.navigationItem.title = @"选择我发布的职位";
    
    UIButton *okBtn = [UIToolsControl initButtonWithFrame:CGRectMake(0, 0, 60*Scale, 44) imageName:nil target:self action:@selector(okBtnTap) title:@"确定" titleColor:THEMECOLOR_OF_ORANGE];
    okBtn.titleLabel.font = Font(Font_15);
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc] initWithCustomView:okBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    
    jobTableView = [UIToolsControl initTableViewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-HeightOfStatusBarAndNavigationBar-SIMEmojKeyBoardkBottomNOSafeArea_Height) target:self style:UITableViewStylePlain];
    jobTableView.tableFooterView = [[UIView alloc] init];
    jobTableView.rowHeight = 50*Scale;
    [self.view addSubview:jobTableView];
    
    __weak __typeof(self)weakSelf = self;
    jobTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        self->currentPage++;
        [self->jobTableView.mj_footer beginRefreshing];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(MJDuration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [weakSelf loadDataForJobList];
        });
    }];
}
- (void)loadDataForJobList {
    SVPHUDShow;
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:0];
    dic = [UIToolsControl getPublicParameter];
    [dic setObject:@"1" forKey:@"status"];
    [dic setObject:[NSString stringWithFormat:@"%ld", (long)currentPage] forKey:@"page"];
    [dic setObject:PageSize forKey:@"number"];
    
    [BaseNetManager requestWithMethod:POST path:Company_GetJob_Lists_URL params:dic successBlock:^(NSDictionary * _Nonnull responseObject) {
        if ([responseObject[@"code"] integerValue] == 2000) {
            id data = responseObject[@"data"];
            if ([data isKindOfClass:[NSArray class]]) {
                for (NSDictionary *dicc in data) {
                    CompanyJobModel *model = [[CompanyJobModel alloc] init];
                    [model setValuesForKeysWithDictionary:dicc];
                    model.isSelect = NO;
                    if (self.jobIdSelect && self.jobIdSelect.length) {
                        if ([model.id isEqualToString:self.jobIdSelect]) {
                            model.isSelect = YES;
                        }
                    }
                    [self->dataArray addObject:model];
                }
                [self->jobTableView reloadData];
            }
            SVPHUDDismiss;
        } else {
            SVPHUDError(responseObject[@"msg"]);
        }
    } failurBlock:^(NSError * _Nonnull error) {
        SVPHUDError(NetErrorAlertText);
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (dataArray.count) {
        return dataArray.count;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    SelectJobCell *cell = [tableView dequeueReusableCellWithIdentifier:isCellID];
    if (!cell) {
        cell = [[SelectJobCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:isCellID];
    }
    
    CompanyJobModel *model = dataArray[indexPath.row];
    [cell reloadDataForModel:model];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    for (NSInteger i = 0; i<dataArray.count; i++) {
        CompanyJobModel *model = dataArray[i];
        if (i == indexPath.row) {
            model.isSelect = YES;
        } else {
            model.isSelect = NO;
        }
    }
    [tableView reloadData];
}

@end
